var vue = null;
$(function() {
    init_vue();
});

function init_vue() {
    vue = new Vue({
        el : "#myapp",
        data : {
            startRow : 0,
            endRow : 0,
            total : 0,
            firstPage : 0,
            lastPage : 0,
            pageNum : 1,
            pageSize : 13,
            list : null,
            setting : null,
            navigatepageNums : null,
            agent_id:0,
            organization_id:'',
            organization_names:'',
            data_cycle:'',
            nick_name:'',
            mobile_phone:'',
            total_balance:0,
            total_recharge:0,
			sortKey:'recharge',
            sort:'desc',
        },
        events : {
            'refreshUserList':function () {
                this.pageNum = 1;
                this.getData();
            },
            'refreshUserInfo': function (t) {
                for (var i=0; i<this.list.length; i++) {
                    if (this.list[i].id == t.user.id) {
                        this.list[i].real_name = t.user.real_name;
                        this.list[i].mobile_phone = t.user.mobile_phone;
                    }
                }
            },
            "selectOrg": function (t) {
                this.organization_id = t.organization_ids;
                this.organization_names = t.organization_names;
            }
        },
        methods : {
            init : function() {
                close_layuiLayerLoad();
                this.initControl();
                this.getData();
            },
            initControl:function () {
                var t = this;
                $('#data_cycle').daterangepicker({
                    type: 2,
                    // drops: 'up',
                    // startDate: moment().subtract('year', 1),
                    startDate: d,
                    endDate: moment(),
                    autoApply:false,
                    autoUpdateInput:true,
                    showDropdowns : true,
                    ranges : {
                        '今日': [moment().startOf('day'), moment()],
                        '昨日': [moment().subtract('days', 1),  moment()],
                        '最近7日': [moment().subtract('days', 6), moment()],
                        '最近30日': [moment().subtract('days', 29), moment()]
                    },
                    opens : 'right', //日期选择框的弹出位置
                    separator : ' to ',
                    locale : {
                        separator:'/',
                        format : 'YYYY-MM-DD', //控件中from和to 显示的日期格式
                        applyLabel : '确定',
                        cancelLabel : '取消',
                        fromLabel : '起始时间',
                        toLabel : '结束时间',
                        customRangeLabel : '自定义',
                        daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                        monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                            '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                        firstDay : 1
                    }
                }, function(start, end, label) {
                    //格式化日期显示框
                    t.data_cycle = start.format('YYYY-MM-DD') + '/' + end.format('YYYY-MM-DD');
                });
            },
            showPage : function(page) {
                // alert(page);
                this.pageNum = page;
                this.getData();
            },
            getData : function() {
                showLoad();
                var t = this;
                postMsg(root+"/user/tolist", {
                    page_num : t.pageNum,
                    page_size : t.pageSize,
                    data_cycle: t.data_cycle,
                    nick_name: t.nick_name,
                    mobile_phone: t.mobile_phone,
                    agent_id:t.agent_id,
                    organization_id:t.organization_id,
					sortKey:t.sortKey,
                    sort:t.sort
                }, function(i) {
                    t.list = null;
                    t.list = i.data.list;
                    t.setting = i.data.setting;
                    t.startRow = i.data.startRow;
                    t.endRow = i.data.endRow;
                    t.total = i.data.total;
                    t.firstPage = i.data.firstPage;
                    t.lastPage = i.data.lastPage;
                    t.navigatepageNums = i.data.navigatepageNums;
                    t.total_balance = i.total_balance;
                    t.total_recharge = i.total_recharge;
                    hideLoad();
                });
            },
            changeState : function(id, state) {
                var t = this;
                showLoad();
                postMsg(root+"/user/changeState", {id:id, state:state}, function(i) {
                    if (i == 1) {
                        for (var i=0; i<t.list.length; i++) {
                            if (t.list[i].id == id) {
                                t.list[i].state = state;
                            }
                        }
                        if (state == 1) {
                            $.scojs_message('账号已停用', $.scojs_message.TYPE_ERROR);
                        } else if (state == 0) {
                            $.scojs_message('账号已启用', $.scojs_message.TYPE_OK);
                        }
                    }
                    hideLoad();
                });
            },
			changeDeallState: function (id, state) {
                var t = this;
                showLoad();
                postMsg(root+"/user/changeState", {id:id, is_deall_order:state}, function(i) {
                    if (i == 1) {
                        for (var i=0; i<t.list.length; i++) {
                            if (t.list[i].id == id) {
                                t.list[i].is_deall_order = state;
                            }
                        }
                        if (state == 2) {
                            $.scojs_message('账号已停止交易', $.scojs_message.TYPE_ERROR);
                        } else if (state == 1) {
                            $.scojs_message('账号已启用交易', $.scojs_message.TYPE_OK);
                        }
                    }
                    hideLoad();
                });
            },
            reset_pass: function (id) {
                layer.confirm('确认要为用户重置密码吗？', {
                    btn: ['确认','取消'] //按钮
                    ,closeBtn: 0
                }, function(index){
                    layer.close(index);
                    showLoad();
                    postMsg(root+"/user/resetPass", {id:id}, function(i) {
                        if (i.code == 1) {
                            $.scojs_message(i.msg, $.scojs_message.TYPE_OK);
                        }
                        hideLoad();
                    });
                });
            },
            query : function () {
                this.pageNum = 1;
                this.getData();
            },
            reset_query : function () {
                agent_id_select.val(null).trigger("change");
                this.agent_id=0;
                this.organization_id='';
                this.organization_names='';
                this.data_cycle=d+'/'+moment().format('YYYY-MM-DD');
                this.nick_name='';
                this.mobile_phone='';
                this.getData();
            },
            openTreeOrganizationWin: function () {
                this.$broadcast('openTreeOrganizationWin');
            },
            exportexcel:function () {
                var t = this;
                t.pageNum = 1;
                t.getData();

                /*data_cycle: t.data_cycle,
                nick_name: t.nick_name,
                mobile_phone: t.mobile_phone,
                agent_id:t.agent_id,
                organization_id:t.organization_id*/

                var url = root + '/user/exportexcel?data_cycle='+t.data_cycle+"&nick_name="+t.nick_name+"&mobile_phone="+t.mobile_phone+"&agent_id="+t.agent_id+"&organization_id="+t.organization_id;

                layer.open({
                    type: 2,
                    title: false,
                    closeBtn: 0, //不显示关闭按钮
                    shade: [0],
                    area: ['0px', '0px'],
                    offset: 'rb', //右下角弹出
                    time: 2000, //2秒后自动关闭
                    anim: 2,
                    content: [url, 'no'] //iframe的url，no代表不显示滚动条
                });
                // window.location.href = root + '/user/exportexcel';
            },
            gotoInfoPage : function (id) {
                window.location.href = root + '#user/info/id/' + id;
            },
            sel_belong : function (user) {
                this.$broadcast('openBelongTreeOrganizationWin', {user:user});
            },
			sortData: function (sortKey) {
                if (this.sortKey == sortKey) {
                    if (this.sort == "desc") {
                        this.sort = "asc";
                    } else {
                        this.sort = "desc";
                    }
                } else {
                    this.sortKey = sortKey;
                    this.sort = "desc";
                }
                this.getData();
            }
        }
    });
    vue.init();
}